<?php

/*
 * 2016.10.17
 * 合作站点类别管理
 */

namespace Qwadmin\Controller;

/**
 * Description of ShopTypesController
 *
 * 合作站点分类管理
 * 2016.10.17
 */
class ShopTypesController extends ComController {
       
   private $shop_sites_id='10001'; // 默认商家Id
   
     public function index()
    {
        $sites_id = I('shop_sites_id');
        if($sites_id){
            $this->shop_sites_id = $sites_id;
        }
        
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('shop_types');
    
        $list = $m->where(array('shop_sites_id'=>$this->shop_sites_id))->select();
//        $page = new \Think\Page($count, $pagesize);
        
//        var_dump($list);die;
        $menus = array();
        foreach( $list as $key => $value ) {
            if( $value['pid'] <= 0 ) {
                $menus[$key] = $value;
                $menus[$key]['children'] = '';
                if( M('shop_types')->where(array('pid' => $value['shop_type_id'],'shop_sites_id'=>$this->shop_sites_id))->count() > 0 ) {
                    $menus[$key]['children'] = 1;
                }
                if(($menus[$key]['shop_sites_id'] && !$menus[$key]['children']) || $this->shop_sites_id=='10001' || $this->shop_sites_id=='10002'){
                      $menus[$key]['goods_name'] = M('goods_type')->where(array('id'=>$menus[$key]['goods_type_id']))->getField('name');  
                    }
              
            }
        }
       
        $customer = M('customer')->select();
        $tree = new \Tree\Tree(M('goods_type')->field('id,pid,name')->select());
        $tree->icon = array('&nbsp;&nbsp;&nbsp;&nbsp;|','&nbsp;&nbsp;&nbsp;&nbsp;|------------','&nbsp;&nbsp;&nbsp;&nbsp;|-');
        $this->assign( 'select_option_list' , $tree->get_tree( 0 , '<option value=\$id \$selected>\$spacer\$name</option>'));
        $this->assign('shop_name',M('customer')->where(array('id'=>$this->shop_sites_id))->getfield('shop_name'));
        $this->assign('customer',$customer);
        $this->assign( 'list' , $menus  );
        $this->assign('htmls', $this->htmls( $list ));
        
        $this->display();
    }
    

    private function htmls ( $list ) {
        $datas = '';
        foreach ( $list as $key => $value ) {
            if( $value['pid'] > 0) {
                $parent_all_data = $this->_getAllParentId( $value['shop_type_id'] ); //过滤没顶级分类的数据
                $parent_all_data = array_reverse( $parent_all_data );
                if( $parent_all_data[0]['pid'] <= 0 ) {
                    $datas[$value['pid']][] = $value;
                }
            }
        }
   
        $html = '';
        foreach( $datas as $key => $value ) {
            if( !$key ) continue;
            $html .="tree_data_2". $this->_getChildren( $key ) ." = {";
            $html .= "'children' : {";
            $i = 0;
            foreach( $value as $k => $v ) {
                $type = 'item';
                $band = '';
                $good_name = '';
                if(M('shop_types')->where(array('pid'=>$v['shop_type_id'],'shop_sites_id'=>$this->shop_sites_id))->find()){
                    $type = 'folder';
                    if($this->shop_sites_id=='10001' || $this->shop_sites_id=='10002'){
                         $band = "<span id=\"status_info\"></span><a  href=\"javascript:edit(\'{$v['id']}\',\'{$v['goods_type_id']}\');\" >绑定中台类别</a>";
                            if($v['goods_type_id']){
                                $name = M('goods_type')->where(array('id'=>$v['goods_type_id']))->getField('name');
                                $good_name = "<span id=\"status_info\">已绑定：".(str_replace("\r\n","",trim($name)))."&nbsp&nbsp </span>";
                                $band = "<a  href=\"javascript:edit(\'{$v['id']}\',\'{$v['goods_type_id']}\');\" >重新绑定</a>";
                            }
                    }
                }else{
                    $band = "<span id=\"status_info\"></span><a  href=\"javascript:edit(\'{$v['id']}\',\'{$v['goods_type_id']}\');\" >绑定中台类别</a>";
                    if($v['goods_type_id']){
                        $name = M('goods_type')->where(array('id'=>$v['goods_type_id']))->getField('name');
                        $good_name = "<span id=\"status_info\">已绑定：".(str_replace("\r\n","",trim($name)))."&nbsp&nbsp </span>";
                        $band = "<a  href=\"javascript:edit(\'{$v['id']}\',\'{$v['goods_type_id']}\');\" >重新绑定</a>";
                    }
                    
                }
              
                $html .= ($i>0?',':''). "'menus{$v['shop_type_id']}' : {text : '".str_replace( "\r\n", "",trim($v['name']))." <span style=\"float:right\" id=\"local_cat_{$v['id']}\" data-id=\"{$vo['id']}\" >{$good_name} {$band}</span>', type : '{$type}' }";
                
                $i++;
            }
            $html .="}}\r\n";
        }
        
//        echo $html;die;
        return $html;
    }
    
    //组合上级节点
    private function _getChildren ( $id  ) {
        $data = $this->_getAllParentId( $id );
        if( !$data ) return;
        $data = array_reverse( $data );
        $string = '';
        foreach( $data as $key => $value ) {
            //. str_replace("\r\n" , '', trim($value['name']))
            $string .= (count($data)>1&&$key>0?'[\'children\']':'').'[\'menus' . $value['shop_type_id'] .'\'][\'additionalParameters\']';
        }
        return $string;
    }
    
    //获得所有上级
    private function _getAllParentId( $id , $data = array() ) {
        $result = M('shop_types')->where(array('shop_type_id' => $id,'shop_sites_id'=>$this->shop_sites_id))->find();
        if( $result ) {
            $data[] = $result;
            if( $result['pid'] > 0 ) {
                $data = $this->_getAllParentId( $result['pid'] , $data );
            }
        }
        return $data;
    }
    
//绑定中台类别
    public function edit($id = 0)
    {
        $goods_type_id = I('goods_type_id');
        $m = M('shop_types');
        $id = I('id');
        $msg ['goods_type_id'] = $goods_type_id;
        $msg ['errno'] = 0;
        $msg ['id'] = $id;
        $msg ['error'] = "绑定成功";
      
        if($id && $goods_type_id){
            
            $flag = $m->data(array('goods_type_id'=>$goods_type_id))->where(array('id'=>$id))->save();
        }
        if (!$flag) {
            $msg ['errno'] = 1;
            $msg ['error'] = "绑定失败！";
            $this->ajaxReturn($msg);
        }
        $msg['name'] = M('goods_type')->where(array('id'=>$goods_type_id))->getField('name');
        $this->ajaxReturn($msg);
    }

   
            
}


